#include "GameAppPch.h"

#include "SectionTest.h"
#include "RenderFacade.h"
#include "RenderUtil.h"
#include "DirectionalLight.h"
#include "PointLight.h"
#include "Vector3.h"
#include "Rect.h"
#include "ResourceManager.h"
#include "MeshStatic.h"

SectionTest::SectionTest(void)
:m_lpTestMesh( NULL )
,m_pLight( NULL )
,m_pMeshStatic( NULL )
{
	m_pLight = new CDirectionalLight();
	Vector3 vDir( 1.0f, 1.0f, 1.0f );
	D3DCOLORVALUE ambient = { 1.0f, 1.0f, 1.0f, 1.0f };
	D3DCOLORVALUE diffuse = { 1.0f, 1.0f, 1.0f, 1.0f };
	D3DCOLORVALUE specular = { 1.0f, 1.0f, 1.0f, 1.0f };

	m_pLight->Init( vDir, diffuse, ambient, specular );

	m_pPointLight = new CPointLight();

	Vector3 vPos( 1.0f, 1.0f, 1.0f );
	m_pPointLight->Init( vPos, 10.0f, diffuse, ambient, specular, 0.0f, 0.5f, 0.0f );

	m_pMeshStatic = new MeshStatic();
	m_pMeshStatic->LoadFromXml( "ObjectData/Onion3.XML" );
}

SectionTest::~SectionTest(void)
{
}

BOOL SectionTest::Init()
{
	if( RenderFacade::GetInstance() )
	{
		HRESULT hr = D3DXCreateTeapot( RenderFacade::GetInstance()->GetDxDevice(), &m_lpTestMesh, NULL );
		if( FAILED( hr ) )
		{
			return FALSE;
		}

		RenderFacade::GetInstance()->SetLight( 0, m_pLight );
		RenderFacade::GetInstance()->SetLight( 1, m_pPointLight );
	}

	return TRUE;
}

void SectionTest::UnInit()
{
	SAFE_DELETE( m_pMeshStatic );

	if( m_pLight )
	{
		delete m_pLight;
		m_pLight = NULL;
	}

	if( m_pPointLight )
	{
		delete m_pPointLight;
		m_pPointLight = NULL;
	}

	if( m_lpTestMesh )
	{
		m_lpTestMesh->Release();
		m_lpTestMesh = NULL;
	}
}

void SectionTest::Update(float TimeDelta)
{
}

void SectionTest::Render()
{
	RenderFacade::GetInstance()->SetRenderState( D3DRS_LIGHTING, FALSE );
	RenderFacade::GetInstance()->SetRenderState( D3DRS_AMBIENT, 0xFFFFFFFF );

	//RenderUtil::GetInstance()->RenderLine( Vector3( 0.0f, 0.0f, 0.0f ), Vector3( 2.0f, 0.0f, 0.0f ), 0xffff0000 );
	//RenderUtil::GetInstance()->RenderLine( Vector3( 0.0f, 0.0f, 0.0f ), Vector3( 0.0f, 2.0f, 0.0f ), 0xff00ff00 );
	//RenderUtil::GetInstance()->RenderLine( Vector3( 0.0f, 0.0f, 0.0f ), Vector3( 0.0f, 0.0f, -2.0f ), 0xff0000ff );
	
	if( m_pMeshStatic )	
	{
		m_pMeshStatic->Render();
	}
}

BOOL SectionTest::HandleEvent(UINT msg, WPARAM wParam, LPARAM lParam)
{
	return TRUE;
}

void SectionTest::ProcessGUIMessage( GUIMessage & msg )
{
}